package com.qs.main.ui;

import android.app.Application;
import android.databinding.ObservableBoolean;
import android.support.annotation.NonNull;

import com.qs.base.data.BaseData;
import com.qs.base.global.SPKeyGlobal;
import com.qs.base.utils.BaseTimeUtils;
import com.qs.base.utils.RetrofitClient;
import com.qs.main.api.ApiService;

import java.util.Date;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;

/**
* 启动页vm
* @Author ltzz
* @Date 2019/9/2
*/
public class StartupPageViewModel extends BaseViewModel {
    public StartupPageViewModel(@NonNull Application application) {
        super(application);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public ObservableBoolean isMember = new ObservableBoolean(false);
    }

    //会员剩余天数
    public void getVIPInfo(String userid) {

        RetrofitClient
                .getInstance()
                .create(ApiService.class)
                .getVIPInfo(BaseData.payName,userid)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) //请求与View周期同步
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) {
                    }
                })
                .subscribe(new Consumer<Map<String, Object>>() {
                    @Override
                    public void accept(Map<String, Object> baseResult) throws Exception {
                        boolean isOk=false;
                        if (baseResult != null) {
                            String day = baseResult.get("day").toString();
                            if (!StringUtils.isTrimEmpty(day)) {
                                SPUtils.getInstance().put(SPKeyGlobal.USER_DATE, day);
                                if (Integer.parseInt(day) > 0) {
                                    isOk = true;
                                    //获取
                                }
                            }else {
                                SPUtils.getInstance().put(SPKeyGlobal.USER_DATE, "0");
                            }
                        }
                        setData(isOk);
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //异常
                    }
                });
    }

    //数据初始化
    public void setData(boolean isOk){
        if (!isOk) {
            //不是会员，初始化识别次数
            Long lastDate = SPUtils.getInstance().getLong(SPKeyGlobal.LAST_DATE, -1L);
            if (lastDate == -1) {
//                第一次赠送次数
                SPUtils.getInstance().put(SPKeyGlobal.TRANSLATE_NUMBER, "10000");
            }
            if (lastDate == -1 || !BaseTimeUtils.lastDateIsThisDateDay(lastDate, new Date().getTime())) {
                SPUtils.getInstance().put(SPKeyGlobal.SEE_NUMBER, "10");
                SPUtils.getInstance().put(SPKeyGlobal.LAST_DATE, new Date().getTime());
            }
        }
    }
}
